package com.zg.generator.web;

import com.zg.common.core.R;
import com.zg.common.core.vo.ColumnVo;
import com.zg.common.web.service.DdlService;
import com.zg.generator.vo.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;

/**
 * @author Zxb
 */
@RestController
@AllArgsConstructor
@Api(tags = {"表管理"})
public class TableController {

    private DdlService ddlService;

    @ApiOperation("查询表")
    @GetMapping("/table/list")
    public R<?> table(String schema, String prefix) {
        List<String> list = ddlService.findTableWithPrefix(schema, prefix);
        return R.success(list);
    }

    @ApiOperation("添加表")
    @PostMapping("/table/add")
    public R<?> addTable(@RequestBody TableAddVo param)  {
        String schema = param.getSchema();
        String tableName = param.getTableName();
        List<ColumnVo> columnVoList = param.getColumnVoList();
        return ddlService.addTable(schema, tableName, columnVoList);
    }

    @ApiOperation("删除表")
    @PostMapping("/table/del")
    public R<?> delTable(@RequestBody TableVo param) {
        String schema = param.getSchema();
        String tableName = param.getTableName();
        return ddlService.delTable(schema, tableName);
    }

    @ApiOperation("查询表字段")
    @GetMapping("/table/column/list")
    public R<?> column(String schema, String tableName) {
        List<ColumnVo> list = ddlService.findColumnByTable(tableName, schema);
        return R.success(list);
    }

    @ApiOperation("删除表字段")
    @PostMapping("/table/column/del")
    public R<?> delColumn(@RequestBody ColumnDelVo param) {
        String schema = param.getSchema();
        String tableName = param.getTableName();
        String columnName = param.getColumnName();
        return ddlService.dropColumn(schema, tableName, columnName);
    }

    @ApiOperation("批量更新或新增表字段")
    @PostMapping("/table/column/merge")
    public R<?> mergeColumn(@RequestBody TableAddVo param) {
        String schema = param.getSchema();
        String tableName = param.getTableName();
        List<ColumnVo> columnVoList = param.getColumnVoList();
        return ddlService.mergeColumnsForTable(schema, tableName, columnVoList);
    }

    @ApiOperation("重置表的公共字段")
    @PostMapping("/table/column/resetPub")
    public R<?> resetPubColumn(@RequestBody TableVo tableVo) {
        String schema = tableVo.getSchema();
        String tableName = tableVo.getTableName();
        ddlService.dropPubColumn(schema, tableName);
        ddlService.addPubColumn(schema, tableName);
        return R.success();
    }

    @ApiOperation("为所有表追加缺失的公共字段")
    @PostMapping("/table/column/repairPub")
    public R<?> repairPubColumn(@RequestBody String schema) {
        List<String> tableList = ddlService.findTable(schema);
        for (String table : tableList) {
            ddlService.addPubColumn(schema, table);
        }
        return R.success();
    }
}
